package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import Client.ClientHandler;
import Client.VCPMain;
import Logic.LoginInfo;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.Font;
import java.io.IOException;

public class Login extends JPanel {
	private ClientHandler client;
	private JTextField userTextField;
	private JTextField passwordTextField;
	private JTextField ipTextField;
	private JTextField portTextField;
	public static LoginInfo loginInfo;
	public static String usern;
	public static String pass;
	public Login(){
		
		initComponent();
	}
	
	public void initComponent(){
		
		//check
setLayout(null);
		
		JLabel lblUserName = new JLabel("User Name:");
		lblUserName.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblUserName.setBounds(102, 101, 99, 20);
		add(lblUserName);
		
		userTextField = new JTextField();
		userTextField.setFont(new Font("Tahoma", Font.PLAIN, 17));
		userTextField.setBounds(206, 91, 146, 30);
		add(userTextField);
		userTextField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblPassword.setBounds(102, 151, 83, 14);
		add(lblPassword);
		
		passwordTextField = new JTextField();
		passwordTextField.setFont(new Font("Tahoma", Font.PLAIN, 17));
		passwordTextField.setBounds(206, 148, 146, 30);
		add(passwordTextField);
		passwordTextField.setColumns(10);
		
		JLabel lblIp = new JLabel("IP");
		lblIp.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblIp.setBounds(102, 192, 46, 14);
		add(lblIp);
		
		ipTextField = new JTextField();
		ipTextField.setFont(new Font("Tahoma", Font.PLAIN, 17));
		ipTextField.setBounds(206, 188, 146, 30);
		add(ipTextField);
		ipTextField.setColumns(10);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblPort.setBounds(102, 237, 46, 14);
		add(lblPort);
		
		portTextField = new JTextField();
		portTextField.setFont(new Font("Tahoma", Font.PLAIN, 17));
		portTextField.setBounds(206, 233, 146, 30);
		add(portTextField);
		portTextField.setColumns(10);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {			// BtnLogin action listener
				int x=0;
				while(x==0);
				usern = userTextField.getText();
				pass= passwordTextField.getText();
				String ip= ipTextField.getText();
				String portStr;
				int port; 
				
				
				if(usern.equals(""))
				{
					JOptionPane.showMessageDialog(null, "Please enter a user name");
					
					
				}
				else if(pass.equals(""))
				{
					JOptionPane.showMessageDialog(null, "Please enter a password");
				}
				if ( !(usern.equals(""))&& !(pass.equals(""))  )
				{
					 loginInfo = new LoginInfo(usern,pass);
					 
				}
			portStr=portTextField.getText();
					
				if(portTextField.getText().equals(""))
					port=5555;
				else
					port=Integer.parseInt(portTextField.getText());
				if(ip.equals(""))
					ip="127.0.0.1";  //set global host to the string ip from text field
				else
					ip=ipTextField.getText();
				
				VCPMain.host=ip;
				VCPMain.port=port;
				ArrayList <String> arrlist=new ArrayList <String>();
				arrlist.add("login");
				arrlist.add(usern);
				arrlist.add(pass);
				try {
					client=new ClientHandler(ip,port);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				VCPMain.chat=client;
				VCPMain.host=ip;
				VCPMain.port=port;
				client.handleMessageFromGui(arrlist);
				Login.this.setVisible(false);
			}
		});
		btnLogin.setBounds(206, 295, 165, 33);
		add(btnLogin);
		
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnExit.setBounds(551, 374, 89, 23);
		add(btnExit);
		
	}
	
	public void setClient(ClientHandler client)
	{
		this.client=client;
	}
	
	public ClientHandler getClient()
	{
		return client;
	}
}
